package servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Box;
import beans.Thread;
import dao.BoxDAO;
import dao.ThreadDAO;

/**
 * Servlet implementation class ForumServlet
 */
public class ForumServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final int NUM_NEW_THREAD = 5;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processer(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processer(request, response);
	}

	private void processer(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ArrayList<Thread> listNewThread = ThreadDAO
				.selectNewThread(NUM_NEW_THREAD);
		request.setAttribute("newThread", listNewThread);

		ArrayList<Box> boxs = BoxDAO.selectAll();

		request.setAttribute("boxs", boxs);
		request.getRequestDispatcher("Forum.jsp").forward(request, response);
	}
}
